//
//  AdressSelectCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/30.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit




class AdressSelectCell: BaseTableViewCell {
    
    weak var delegateVC : AdressSelectController?

    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var phoneLabel: UILabel!
    
    @IBOutlet weak var adressLabel: UILabel!
    
    @IBOutlet weak var defulLabel: UILabel!
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var leftMargin: NSLayoutConstraint!
    
    @IBOutlet weak var deleBtn: UIButton!
    var didSelectID : String?
    
    var model : AddressInfoModel?{
        
        didSet{
            
            if model == nil {
                return
            }
            
            nameLabel.text = model?.true_name
            
            phoneLabel.text = model?.mob_phone
            
            
            adressLabel.text = model!.area_info + model!.address
            
            if didSelectID == model!.address_id{
                
                nameLabel.textColor = UIColor.red
                
                phoneLabel.textColor = UIColor.red
                
                deleBtn.isHidden = true
                
                leftImageView.isHidden = false
                
                leftMargin.constant = 32

            } else {
                
                nameLabel.textColor = UIColor.black
                
                phoneLabel.textColor = UIColor.black
                
                deleBtn.isHidden = false

                leftMargin.constant = 8
                
                leftImageView.isHidden = true

            }
            
            

            
            
            if model?.is_default == "1" {
                
                defulLabel.isHidden = false
                
            }else{
                
                defulLabel.isHidden = true


                
            }

            
            
        }
    
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.white
        nameLabel.font = UIFont.boldSystemFont(ofSize: 15)
        phoneLabel.font = UIFont.boldSystemFont(ofSize: 15)
        defulLabel.addCornerAndBorder(borderColor: UIColor.blue, borderWidth: 0.6, masksToBounds: true, cornerRadius: 0)
        
        adressLabel.textColor = UIColor.dakGray()
        // Initialization code
    }
    
    
    @IBAction func btnDidTap(_ sender: UIButton) {
        
        
        let tempVC = UIAlertController(title: nil, message: "确定删除此地址吗?", preferredStyle: .alert)
        
        weak var weakSelf = self
        
        let action1 = UIAlertAction(title: "确定", style: .destructive) { (action) in
            
            
            var parmeter  = [String : AnyObject]()
            
            parmeter["address_id"] = weakSelf!.model!.address_id as AnyObject
            
            NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_address&a=address_del", parameters: parmeter) { (_, error) in
            
                if error == nil{
                
                 weakSelf?.delegateVC?.loadAdressListData()
                
                
                }
                    
            }
            
        }
        
        
        
        let action2 = UIAlertAction(title: "取消", style: .default) { (action) in
            
            
            
        }
        
        tempVC.addAction(action2)
        tempVC.addAction(action1)
        
        viewController().present(tempVC, animated: true, completion: nil)
        
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    deinit {
        QL1("鲨")
    }
    
}
